package me.linkcn.ydtxl.server;

import me.linkcn.ydtxl.server.data.jdbc.JdbcFacade;
import me.linkcn.ydtxl.server.objects.ObjectsFacade;
import me.linkcn.ydtxl.server.resources.AccountResource;
import me.linkcn.ydtxl.server.resources.GroupResource;
import me.linkcn.ydtxl.server.resources.GroupsResource;
import org.restlet.Restlet;
import org.restlet.routing.Router;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: cmcc
 * Date: 11-8-16
 * Time: 下午11:29
 */
public class Application extends org.restlet.Application {
    private final ObjectsFacade dataFacade;
    private final String dbUri;

    public static Properties getProperties(String propertiesUri)
            throws IOException {
        InputStream is = Application.class.getResourceAsStream(propertiesUri);
        Properties properties = new Properties();
        properties.load(is);
        return properties;
    }

    public Application() throws IOException {
        Properties properties = getProperties("application.properties");
        this.dbUri = properties.getProperty("db.uri");
        this.dataFacade = new ObjectsFacade(new JdbcFacade(this.dbUri));
    }

    public ObjectsFacade getObjectsFacade() {
        return this.dataFacade;
    }

    @Override
    public Restlet createInboundRoot() {
        final Router router = new Router(getContext());
        router.attach("/account", AccountResource.class);
        router.attach("/{accountName}/groups", GroupsResource.class);
        //router.attach("/{accountName}/group", GroupResource.class);
        router.attach("/{accountName}/group/{groupName}", GroupResource.class);
        return router;
    }
}
